/*
 *  SableJBDD, a Java Binary Decision Diagram package.
 *  Copyright (C) 2003  Feng Qian
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
//$Id: JBddNode.java,v 1.17 2004/06/03 03:54:02 fqian Exp $

package SableJBDD.bdd;

import SableJBDD.bdd.internal.UniqueObject;
/**
 * A class for bdd internal nodes. 
 *
 * @author Feng Qian
 */

public abstract class JBddNode extends UniqueObject{
  
  public JBddVariable variable;
  public JBddNode low;
  public JBddNode high;

  /**
   * @return true if this is an internal node (not 0 or 1)
   */
  abstract boolean isConst(); 

  /**
   * @return the order of the node
   */
  abstract int getLevel();

  /**
   * Reclaims the node object, only called by node pool.
   * A node can only be recliamed when it is weakly reachable 
   * (or phantom node), which means there is not regular references
   * to this node. In this case, the unique id and object can be
   * reused without a problem.
   */
  public void reclaim() {
    this.variable = null;
    this.low      = null;
    this.high     = null;
  }

  /**
   * Mutates a reclaimed node to a new instance.
   * @param newv the new variable
   * @param newl the new low branch
   * @param newh the new high branch
   */
  public void mutate(JBddVariable newv, JBddNode newl, JBddNode newh) {
    assert (this.variable == null) && (this.low == null) && (this.high == null); 
    this.variable = newv;
    this.low      = newl;
    this.high     = newh;
  }

  /**
   * Checks if this bdd node matches (v, l, h) triple.
   */
  public boolean matches(JBddVariable v, JBddNode l, JBddNode h) {
    return (this.variable == v && this.low == l && this.high == h);
  }

  /////////////// for other purpose
  JBddNode next;

  public final void setNext(JBddNode next) {
    this.next = next;
  }

  public final void clearNext() {
    this.next = null;
  }

  public final JBddNode getNext() {
    return this.next;
  }
}
